package com.rxb.blog.controller;

import com.rxb.blog.common.CommonResult;
import com.rxb.blog.entity.ArticlePraiseEntity;
import com.rxb.blog.service.ArticleInfoService;
import com.rxb.blog.vo.ArticleInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Author：rxb
 * Date：2020-02-26 16:37
 * Description：博客相关接口
 */
@Api(description = "博客相关接口")
@RestController
@RequestMapping("article")
public class ArticleInfoController {

    @Autowired
    private ArticleInfoService articleInfoService;

    @ApiOperation(value = "写博客/更新博客", notes="写博客/更新博客")
    @ApiImplicitParam(name = "articleInfoVo", value = "博客实体vo", paramType = "query", required = true, dataType = "ArticleInfoVo")
    @PostMapping("insertArticle")
    public void insertArticle(@RequestBody ArticleInfoVo articleInfoVo, HttpServletRequest request ){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        articleInfoVo.setUsername(username);
        articleInfoService.insertArticle(articleInfoVo);
    }

    @ApiOperation(value = "获取自己的博客名称列表", notes="获取自己的博客名称列表")
    @ApiImplicitParam(name = "articleInfoVo", value = "博客实体vo", paramType = "query", required = true, dataType = "ArticleInfoVo")
    @PostMapping("getMyArticleList")
    public CommonResult<Map> getMyArticleList(@RequestBody ArticleInfoVo articleInfoVo, HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        articleInfoVo.setUsername(username);
        Map myArticleList = articleInfoService.getMyArticleList(articleInfoVo);
        return CommonResult.success(myArticleList);
    }

    @ApiOperation(value = "根据id查询博客详情信息", notes="根据id查询博客详情信息")
    @ApiImplicitParam(name = "id", value = "博客详情id", paramType = "query", required = true, dataType = "Integer")
    @GetMapping("getMyArticleDetail")
    public CommonResult<ArticleInfoVo> getMyArticleDetail(@RequestParam("id") Integer id){
        ArticleInfoVo articleInfoVo = articleInfoService.getArticleDetail(id);
        return CommonResult.success(articleInfoVo);
    }

    @ApiOperation(value = "根据id删除博客", notes="根据id删除博客")
    @ApiImplicitParam(name = "articleInfoVo", value = "博客实体vo", paramType = "query", required = true, dataType = "ArticleInfoVo")
    @PostMapping("deleteArticle")
    public void deleteArticle(@RequestBody ArticleInfoVo articleInfoVo){
        articleInfoService.deleteArticle(articleInfoVo.getId());
    }

    @ApiOperation(value = "自己的博客置顶操作", notes="自己的博客置顶操作")
    @ApiImplicitParam(name = "articleInfoVo", value = "博客实体vo", paramType = "query", required = true, dataType = "ArticleInfoVo")
    @PostMapping("toTop")
    public void toTop(@RequestBody ArticleInfoVo articleInfoVo){
        articleInfoService.toTop(articleInfoVo.getId());
    }

    @ApiOperation(value = "自己的博客置顶操作", notes="自己的博客置顶操作")
    @ApiImplicitParam(name = "articleInfoVo", value = "博客实体vo", paramType = "query", required = true, dataType = "ArticleInfoVo")
    @PostMapping("cancleTop")
    public void cancleTop(@RequestBody ArticleInfoVo articleInfoVo){
        articleInfoService.cancleTop(articleInfoVo.getId());
    }

    @ApiOperation(value = "获取首页博客列表（分页暂时不做）", notes="获取首页博客列表（分页暂时不做）")
    @ApiImplicitParam(name = "articleTypeName", value = "博客类型", paramType = "query", required = true, dataType = "String")
    @GetMapping("getArticleListWithType/{articleTypeName}")
    public CommonResult<List<Map>> getArticleListWithType(@PathVariable("articleTypeName") String articleTypeName) {
        List<Map> articleTypes = articleInfoService.getArticleListWithType(articleTypeName);
        return CommonResult.success(articleTypes);
    }

    @ApiOperation(value = "点赞、取消点赞、踩、取消踩", notes="点赞、取消点赞、踩、取消踩")
    @ApiImplicitParam(name = "map", value = "", paramType = "query", required = true, dataType = "Map")
    @PostMapping("praise")
    public CommonResult<String> praise(@RequestBody Map<String,Object> map){
        try {
            articleInfoService.praise(map);
            return CommonResult.success("成功");
        }catch (RuntimeException e){
            return CommonResult.failed("失败");
        }
    }

    @ApiOperation(value = "获取点赞、取消点赞、踩、取消踩", notes="获取点赞、取消点赞、踩、取消踩")
    @ApiImplicitParam(name = "map", value = "", paramType = "query", required = true, dataType = "Map")
    @PostMapping("getPraise")
    public CommonResult<ArticlePraiseEntity> getPraise(@RequestBody Map<String,Object> map){
        ArticlePraiseEntity praise = articleInfoService.getPraise(map);
        return CommonResult.success(praise);
    }
}
